<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" ref="searchForm" :model="searchForm">
            <el-form-item prop="msgType" label="设备类型:">
              <el-select v-model="searchForm.equipType" :clearable="true" placeholder="请选择设备类型">
                <el-option v-for="item in equipmentIsTypeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="msgType" label="设备编号:">
              <el-input v-model="searchForm.equipCode" placeholder="请输入设备编号" clearable></el-input>
            </el-form-item>

            <el-form-item prop="msgType" label="报警类型:">
              <el-select v-model="searchForm.alarmType" :clearable="true" placeholder="请选择报警类型">
                <el-option v-for="item in alarmTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="msgType" label="报警状态:">
              <el-select v-model="searchForm.alarmState" :clearable="true" placeholder="请选择报警状态">
                <el-option label="报警中" :value="1"></el-option>
                <el-option label="已解除" :value="2"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="msgType" label="报警方式:">
              <el-select v-model="searchForm.taskOrigin" :clearable="true" placeholder="请选择报警方式">
                <el-option v-for="item in EAlarmOrigin()" :label="item.label" :value="item.value" :key="item.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="date" label="报警时间:">
              <el-date-picker
                v-model="searchForm.date"
                :clearable="true"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table ref="tableList" :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="alarmCreateDate" label="报警时间" align="center" />
        <el-table-column prop="taskOrigin" label="报警方式" align="center" :formatter="handleFormatterAlarmTaskOrigin" />
        <el-table-column prop="equipType" label="设备类型" align="center" :formatter="handleFormatterEquipType" />
        <el-table-column prop="equipType" label="设备类型" align="center" :formatter="handleFormatterEquipType" />
        <el-table-column prop="equipCode" label="设备编号" align="center" />
        <el-table-column prop="alarmType" label="报警类型" align="center" :formatter="handleFormatterAlarmType" />
        <el-table-column label="报警状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.alarmState === 2" size="mini" effect="dark" type="success"> 正常 </el-tag>
            <el-tag v-if="scope.row.alarmState === 1" size="mini" effect="dark" type="danger"> 报警 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="cancelReason" label="解除原因" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.cancelReason === 2" size="mini" effect="dark" type="success"> 已处理 </el-tag>
            <el-tag v-if="scope.row.cancelReason === 1" size="mini" effect="dark" type="danger"> 误触发 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="alarmModifyDate" label="解除时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.alarmModifyDate || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionCheck" @click="handleViewInfo(scope.row.taskId)" size="mini">查看</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import { EAlarmTypeList, EquipmentIsTypeList, EAlarmOrigin } from '@/enum';
import MessageApi from '@/api/message/message';
export default {
  name: 'NotifyTaskManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      searchForm: {},
      alarmTypeList: EAlarmTypeList,
      equipmentIsTypeList: EquipmentIsTypeList,
      tableList: [],

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },
      permissionCheck: 'SYSTEM_MANAGER_MESSAGE_NOTIFY_TASK_LIST_VIEW',
    };
  },
  mounted() {
    this.getTableHeight();
    this.listTable();
    window.addEventListener('keydown', this.keyDown);
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    EAlarmOrigin() {
      return EAlarmOrigin;
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.searchForm = this.$options.data().searchForm;
      this.onSearch();
    },
    listTable() {
      this.$emit('show-loading', true);
      const { pageNo, pageSize } = this.page;
      const data = { ...this.searchForm, pageNo, pageSize };
      if (this.searchForm.date && this.searchForm.date.length === 2) {
        data.alarmBeginDate = this.$Utils.formatDate(this.searchForm.date[0], 'yyyy-MM-dd hh:mm:ss');
        data.alarmEndDate = this.$Utils.formatDate(this.searchForm.date[1], 'yyyy-MM-dd hh:mm:ss');
      }

      MessageApi.notifyTaskPage(data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.$refs.tableList.bodyWrapper.scrollTop = 0;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          this.$emit('show-loading', false);
        });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode === 13) {
        this.onSearch();
      }
    },

    /**
     * 设备类型匹配
     * @param row
     * @param column
     * @param cellValue
     */
    handleFormatterEquipType(row, column, cellValue) {
      return EquipmentIsTypeList.find((c) => c.id === cellValue).name;
    },
    /**
     * 报警方式匹配
     * @param row
     * @param column
     * @param cellValue
     */
    handleFormatterAlarmTaskOrigin(row, column, cellValue) {
      return EAlarmOrigin.find((c) => c.value === cellValue).label;
    },
    /**
     * 报警类型匹配
     * @param row
     * @param column
     * @param cellValue
     */
    handleFormatterAlarmType(row, column, cellValue) {
      return EAlarmTypeList.find((c) => c.value === cellValue).label;
    },
    /**
     * 查看
     */
    handleViewInfo(taskId) {
      this.$router.push({
        path: '/message/noticeTaskDetail?taskId=' + taskId,
      });
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content';
</style>
